//
//  GameStateGraphics.m
//  puzzle
//
//  Created by omega on 09/07/24.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "GameStateGraphics.h"
#import "GameState.h"
#import "hellApp.h"
#import "Effect.h"

extern PlayState play;
extern int continuing_chain_counter;
extern int chain_counter;
extern int continuing_counter;
extern bool grab;
extern int next_line_drop_x;


void draw_panel( Panel_Type t , int alpha ){	
	Hell_drawTextureColor( HELL_TEXTURE_BOX , PANEL_SIZE * (int)t , 0 , PANEL_SIZE , PANEL_SIZE , 255,255,255, alpha);
}

void draw_chain_counter(){
	static int prev_view_counter;
	static float sz = 1.0f;
	int player_timer = play.player_timer;
	char buf[1024];
	int r[] = {   0 , 255 };
	int g[] = {   0 , 255 };
	int b[] = {   0 , 255 };
	
	sz = sz * 0.8f + 0.2f;
	if( prev_view_counter != chain_counter ){
		prev_view_counter = chain_counter;
		sz = 2.0f;
	}
	glPushMatrix();
	sprintf( buf, "+%d" , chain_counter );
	glTranslatef( 0 , -128 , 0 );
	glTranslatef( -((int)strlen(buf)-1) * fontparam[ HELL_FONTPARAM_BOX ].draw_w/2.0f * sz , 0 , 0 );
	glScalef( sz , sz , 0 );
	Hell_drawFontWithTextureColor( buf , fontparam[ HELL_FONTPARAM_BOX ] , r[player_timer % 2] , g[player_timer % 2] , b[player_timer % 2], 240);
	glPopMatrix();
	
	glPushMatrix();
	sprintf( buf, "+%d" , get_gain_score_for_view( chain_counter , continuing_chain_counter , continuing_counter ) );
	glTranslatef( 0 , -128 -16-8 , 0 );
	glTranslatef( -((int)strlen(buf)-1) * fontparam[ HELL_FONTPARAM_BOX ].draw_w/4.0f * sz , 0 , 0 );
	glScalef( sz/2 , sz/2 , 0 );
	Hell_drawFontWithTextureColor( buf , fontparam[ HELL_FONTPARAM_BOX ] , r[player_timer % 2] , g[player_timer % 2] , b[player_timer % 2], 240);
	glPopMatrix();
	
}

void draw_continuing_chain_counter(){
	static int prev_view_counter;
	static float sz = 1.0f;
	int player_timer = play.player_timer;
	char buf[1024];
	int r[] = {   0 , 255 };
	int g[] = {   0 , 255 };
	int b[] = {   0 , 255 };
	
	sz = sz * 0.8f + 0.2f;
	if( prev_view_counter != continuing_chain_counter ){
		prev_view_counter = continuing_chain_counter;
		sz = 2.0f;
	}
	glPushMatrix();
	glTranslatef( 0 , -128 -16-8*3 , 0 );
	sprintf( buf, "%d" , continuing_chain_counter );
	glTranslatef( -((int)strlen(buf)-1) * fontparam[ HELL_FONTPARAM_BOX ].draw_w/4.0f * sz , 0 , 0 );
	glScalef( sz/2 , sz/2 , 0 );
	Hell_drawFontWithTextureColor( buf , fontparam[HELL_FONTPARAM_BOX] , r[player_timer % 2] , g[player_timer % 2] , b[player_timer % 2], 240);
	glPopMatrix();
}


void GameStateGraphics_draw(){
	char buf[1024];
	int x,y;
	int player_timer = play.player_timer;
	
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrthof(-160.0f, 160.0f, -240.0f, 240.0f, -1.0f, 1.0f);
    glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	
	glEnable(GL_TEXTURE_2D);
	// draw background
	Hell_drawTexture( HELL_TEXTURE_BACKGOUND, 0 , 0, 320, 480 );
	
	if( continuing_counter > 1 ){
		for(int t=-1;t<3;t++){
			glPushMatrix();
			glTranslatef( -160+16 , 0 + (player_timer*10) % (32*8) - 32*8*t , 0 );
			glRotatef( -90 , 0 , 0 , 1 );
			Hell_drawTextureColor( HELL_TEXTURE_FONTBOX , 32*0 , 32*7 , 32*8 , 32 ,255,255,255,128);
			glPopMatrix();
			
			glPushMatrix();
			glTranslatef( 160-16 , 0 + (player_timer*10) % (32*8) - 32*8*t , 0 );
			glRotatef( -90 , 0 , 0 , 1 );
			Hell_drawTextureColor( HELL_TEXTURE_FONTBOX , 32*0 , 32*7 , 32*8 , 32 ,255,255,255,128);
			glPopMatrix();
		}
	}
	
	// draw guide of next_line
	{
		static float guide_alpha = 0;
		if( grab ) {
			guide_alpha = guide_alpha * 0.8 + 1.0 * 0.2;
		}else{
			guide_alpha = 0;
		}
		for(x = 0; x < PANEL_FIELD_W ; x++){
			if( x + next_line_drop_x >= PANEL_FIELD_W || play.next_line[x][0] == PANEL_TYPE_NONE) continue;
			
			glPushMatrix();
			glTranslatef( (x + next_line_drop_x) * PANEL_SIZE + PANEL_NEXT_OFFSET_X , 0 , 0 );
			glScalef( 1 , -480.0f / PANEL_SIZE  , 1 );
			Hell_drawTextureColor( HELL_TEXTURE_BOX , PANEL_SIZE * (int)play.next_line[x][0] , PANEL_SIZE , PANEL_SIZE , PANEL_SIZE , 255,255,255, 128 * guide_alpha);
			glPopMatrix();
		}
	}
	
	// draw field
	for(x = 0; x < PANEL_FIELD_W ; x++){
		for(y = 0 ; y < PANEL_FIELD_H ; y++){
			if( play.panel[x][y].type == PANEL_TYPE_NONE ) continue;
			
			float ofs = (play.panel[x][y].state == PANEL_STATE_FALLING) ? play.panel[x][y].fall_diff * PANEL_SIZE : 0;
			
			glPushMatrix();
			glTranslatef(x * PANEL_SIZE + PANEL_FIELD_OFFSET_X , (PANEL_FIELD_H - y + play.raised_field) * PANEL_SIZE + PANEL_FIELD_OFFSET_Y - ofs, 0 );
			if( play.panel[x][y].state == PANEL_STATE_ERASING){
				if(play.erasing_timer > PANEL_ERASE_TIME_AFFECT ){
					int t = play.erasing_timer - PANEL_ERASE_TIME_AFFECT;
					glRotatef( t*t* 0.1 , 0 , 0 , 1 );
				}else{
					if( play.panel[x][y].timer < PANEL_ERASE_TIME_AFFECT ){
						float t = (float)(PANEL_ERASE_TIME_AFFECT - play.panel[x][y].timer) / PANEL_ERASE_TIME_AFFECT + 1.0f;
						glScalef( t , t , 0 );
					}
				}
			}
			draw_panel( (int)play.panel[x][y].type , 255 );
			
			glPopMatrix();
		}
	}
	
	// draw erasing effect
	Hell_setAlphaBlendMode( HELL_ALPHABLEND_ADD );
	for(x = 0; x < PANEL_FIELD_W ; x++){
		for(y = 0 ; y < PANEL_FIELD_H ; y++){
			if( play.panel[x][y].type == PANEL_TYPE_NONE ) continue;
			if( play.panel[x][y].state == PANEL_STATE_ERASING && (player_timer+x+y) % 2 ){
				float ofs = (play.panel[x][y].state == PANEL_STATE_FALLING) ? play.panel[x][y].fall_diff * PANEL_SIZE : 0;
				
				glPushMatrix();
				glTranslatef(x * PANEL_SIZE + PANEL_FIELD_OFFSET_X , (PANEL_FIELD_H - y + play.raised_field) * PANEL_SIZE + PANEL_FIELD_OFFSET_Y - ofs, 0 );
				Hell_drawTextureColor( HELL_TEXTURE_BOX , 0.0f , 128.0f , 64.0f , 64.0f , rand()%255 ,rand()%255 ,rand()%255 ,255 );
				glPopMatrix();
			}
		}
	}
	Hell_setAlphaBlendMode( HELL_ALPHABLEND_NORMAL );
	
	// draw rear bar of next_line blocks
	glPushMatrix();
	glTranslatef( 0 , 240-32 , 0 );
	glRotatef( 90 , 0 , 0 , 1 );
	Hell_drawTexture( HELL_TEXTURE_BACKGOUND , 320 , 0 , 64 , 320 );
	glPopMatrix();
	
	
	// draw next_line blockes
	{
		static float ofs = 0;
		if( grab ){
			ofs = ofs * 0.8 + (-PANEL_SIZE / 2) * 0.2;
		}else{
			ofs = 0;
		}
		
		// queue
		for(int d = PANEL_NEXT_QUEUE_LEN-1 ; d > 0 ; d-- ){
			for(x = 0; x < PANEL_FIELD_W ; x++){
				if(play.next_line[x][d] == PANEL_TYPE_NONE) continue;
				glPushMatrix();
				glTranslatef( x * PANEL_SIZE + PANEL_NEXT_OFFSET_X , PANEL_NEXT_OFFSET_Y + ofs + d * PANEL_SIZE / 2, 0 );
				draw_panel( play.next_line[x][d] , 255 );
				glPopMatrix();
			}
		}
		
		for(x = 0; x < PANEL_FIELD_W ; x++){
			if( x + next_line_drop_x >= PANEL_FIELD_W || play.next_line[x][0] == PANEL_TYPE_NONE) continue;
			
			glPushMatrix();
			glTranslatef( (x + next_line_drop_x) * PANEL_SIZE + PANEL_NEXT_OFFSET_X , PANEL_NEXT_OFFSET_Y + ofs , 0 );
			draw_panel( play.next_line[x][0] , 255 );
			glPopMatrix();
		}
	}
	
	// draw effect
	GameState_drawEffect();
	
	Hell_setAlphaBlendMode( HELL_ALPHABLEND_NORMAL );
	
	// draw score
	glPushMatrix();
	sprintf( buf , "%d" , play.score );
	glTranslatef( -160+8 , 240-8 , 0 );
	glScalef( 0.5 ,  0.5 , 1 );
	Hell_drawFontWithTextureColor( buf , fontparam[ HELL_FONTPARAM_BOX ] , 0,0,0,128 );
	glPopMatrix();
	
	glPushMatrix();
	sprintf( buf , "%d" , play.cleared_panel_counter );
	glTranslatef( -160+8 , 240-24 , 0 );
	glScalef( 0.5 ,  0.5 , 1 );
	Hell_drawFontWithTextureColor( buf , fontparam[ HELL_FONTPARAM_BOX ] , 0,0,0,128 );
	glPopMatrix();
	
	glPushMatrix();
	sprintf( buf , "%d" , play.scrolled_dist );
	glTranslatef( 160+8 - strlen( buf ) * 16 , 240-8 , 0 );
	glScalef( 0.5 ,  0.5 , 1 );
	Hell_drawFontWithTextureColor( buf , fontparam[ HELL_FONTPARAM_BOX ] , 0,0,0,128 );
	glPopMatrix();
	
	if( continuing_chain_counter > 0 && chain_counter > 0 ) draw_continuing_chain_counter();
	if( chain_counter > 0 ) draw_chain_counter();
}